<template>
    <tnxel-submit-form :model="model">
        <div class="flex-v-center mb-3">
            <el-checkbox class="me-0" v-model="model.lombok" @change="refresh(true)">使用Lombok</el-checkbox>
            <span class="form-item-tip">不推荐使用Lombok，如果确实要使用，也仅限生成Getter和Setter方法</span>
            <el-checkbox class="ms-4" v-model="model.extended" @change="refresh(true)" v-if="extendable">
                继承父类的查询模型
            </el-checkbox>
        </div>
        <el-form-item label="文件" label-width="44px">{{ model.path }}</el-form-item>
        <tnxel-alert type="error" class="mb-0" v-if="model.error">{{ model.error }}</tnxel-alert>
        <template v-else-if="model.code">
            <CodePreview :code="model.code"/>
            <div class="flex-v-center justify-content-end mt-3">
                <tnxel-button icon="fa-solid fa-floppy-disk" @click="save">
                    保存查询模型代码到文件
                </tnxel-button>
                <tnxel-button icon="fa-solid fa-copy" @click="copy">
                    复制查询模型代码到剪切板
                </tnxel-button>
            </div>
        </template>
    </tnxel-submit-form>
</template>

<script>
import tnx from '@/tnx';
import ModelingApp from '@/logic/modeling/entity/ModelingApp.js';
import EntityClass from '@/logic/modeling/entity/EntityClass.js';
import PersistUtil from '@/logic/modeling/util/persist.js';
import GenerateUtil from '@/logic/modeling/util/generate.js';

import CodePreview from './components/CodePreview.vue';

export default {
    components: {
        CodePreview,
    },
    props: {
        app: {
            type: ModelingApp,
            required: true,
        },
        clazz: {
            type: EntityClass,
            required: true,
        },
    },
    data() {
        return {
            model: {
                path: null,
                lombok: false,
                extended: GenerateUtil.getDefaultExtended(this.app, this.clazz),
                code: null,
                error: null,
            },
        };
    },
    computed: {
        project() {
            return tnx.app.global.modelingProject;
        },
        extendable() {
            let superClass = this.app.findClass(this.clazz.superClassId, true, true);
            if (superClass) {
                return superClass.kind.equals(this.clazz.kind);
            }
            return false;
        },
    },
    mounted() {
        this.refresh();
    },
    methods: {
        refresh(enforced) {
            if (!this.model.path) {
                PersistUtil.loadRelativeMavenProject(this.app.path).then(mavenProject => {
                    let root = tnx.util.file.getParentDir(this.app.path);
                    this.model.path = GenerateUtil.getPath(root, mavenProject, this.clazz, 'querying');
                });
            }
            if (enforced || !this.model.code) {
                this.model.code = GenerateUtil.generateQueryingCode(this.app, this.clazz, {
                    lombok: this.model.lombok,
                    extended: this.model.extended,
                });
            }
        },
        save() {
            GenerateUtil.save(this.model.path, this.model.code, 'querying');
        },
        copy() {
            GenerateUtil.copy(this.model.code);
        },
    }
}
</script>

<style lang="scss" scoped>
</style>
